import { useCallback } from "react"
import { useSelector, useDispatch } from "react-redux"
import shoppingAPI from "../api/shopping"
import Creators from "../store/actions"
export const useShopping = () => {
    const shoppings = useSelector(state => state.shoppings);
    const dispatch = useDispatch();
    const { curPage, eachPage } = shoppings;
    const getshopping = useCallback(async () => {
        const data = await shoppingAPI.get({ curPage, eachPage })
        dispatch(Creators.getShoppingsshop(data));
    }, [curPage, eachPage, dispatch]);

    const setCurpage = useCallback(curPage => {
        console.log("setCurpage");
        dispatch(Creators.setCurPageshop({ curPage }));
    }, [dispatch])

    const seteachs = useCallback(eachPage => {
        dispatch(Creators.setEachpageshop({ eachPage }))
    }, [dispatch]);

    //  加入购物车
    const addCart = useCallback(async ({ num, userid, shoppingid }) => {
        const data = { num, userid, shoppingid }
        const result = await shoppingAPI.add(data);
        return result
    },[])
    return [shoppings, { getshopping, setCurpage, seteachs,addCart }]
}